/**
 * Created by steve on 14-9-18.
 */
define(['jquery', 'dialogs'], function ($, dia) {
//var url_base = 'https://120.24.55.7:8449/staff/';
//  var url_base = 'http://120.24.55.7:9191/staff/';
   var url_base = 'http://192.168.2.222:8080/staff/';


var escapeHTML = (function() {

var chars = {
'"': '&quot;',
"'": '&#39;',
'&': '&amp;',
'/': '&#47;',
'<': '&lt;',
'>': '&gt;'
};

return function(text) {
    return String(text).replace(/[\"'\/<>]/g, function(char) {
        return chars[char];
    });
};

}());

function removeEmptyvalueJson(parameters){
    var res={};
    for(var o in parameters){
        if(parameters[o]=="" || parameters[o] == undefined){
            continue;
        }else{
        // escapeHTML
        // res[o] = parameters[o];
        res[o] = escapeHTML(parameters[o]);
        }
    }
    return res;
}

    return {
        get: function (url, parameters, fail, success) {
            console.log('---请求参数---');
            console.log('url:');
            console.log(url);
            console.log('parameters:');
            console.log(parameters);

			if (localStorage.getItem('login_user')!=null && !parameters.userId) {
                var user = JSON.parse(localStorage.getItem('login_user'));
				parameters.userId = user.userId;
            }

			var rparms = removeEmptyvalueJson(parameters);

            // 链接参数到url
            var full_url = url_base + url + "?";
            var i = 0;
            $.each(rparms, function (key, value) {
                if (i == 0) {
                    full_url += key + "=" + value;
                } else {
                    full_url += "&" + key + "=" + value;
                }
            });

            // GET
            $.ajax({
                type: 'GET',
                url: full_url,
                dataType: 'json'
                /*beforeSend: function(xhr) {
                    //Don't use this, Android will fail to send https request
                    if (localStorage.getItem('login_user')!=null) {
                        var user = JSON.parse(localStorage.getItem('login_user'));
                        console.error('user');
                        console.error(user);
                        xhr.setRequestHeader('BasicAuthUsername', user.userId);
                    }
                }*/
            }).fail(function (error) {
                console.error(error);
                dia.alert('Confirmation', 'No internet connection. Please check your network.', ['OK'], function (title) {
                });
                fail('Please check your network.');
            }).done(function (response) {
                console.log(response);
                success(response);
            });
        },

        post: function (url, parameters, fail, success,jsonParam) {

            if(!jsonParam){
                jsonParam = {};
            }

            if(typeof(jsonParam.loading) == "undefined"){
//                $(".ui-loader").show();
                $(".loadingDiv").addClass("active");
            }
            console.log('---POST Request---');
            console.log('url: %o', url);
            console.log('parameters: %o', parameters);

			if (localStorage.getItem('login_user')!=null && !parameters.userId) {
                var user = JSON.parse(localStorage.getItem('login_user'));
				parameters.userId = user.userId;
            }

			var asyncPra = true;
            if(typeof(jsonParam.async) != "undefined"){
                asyncPra = jsonParam.async;
            }
			var rparms = removeEmptyvalueJson(parameters);
            // POST
            $.ajax({
                type: 'POST',
                url: url_base + url,
                dataType: 'json',
                async: asyncPra,
                data: rparms
//                beforeSend: function(xhr) {
//                    //Don't use this, Android will fail to send https request
////                    if (localStorage.getItem('login_user')!=null) {
////                        var user = JSON.parse(localStorage.getItem('login_user'));
////                        xhr.setRequestHeader('BasicAuthUsername', user.userId);
////                    }
////                    xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
////                    2.请求页面html，加入<meta http-equiv="Access-Control-Allow-Origin" content="*">
//                }
            }).fail(function (error) {
                console.error(error);
//                $(".ui-loader").hide();
                if(typeof(jsonParam.loading) == "undefined") {
                    $(".loadingDiv").removeClass("active");
                }
                dia.alert('Confirmation', 'No internet connection. Please check your network.', ['OK'], function (title) {
                });
                fail('Please check your network.');
            }).done(function (response) {
                console.log('POST Response: %o', response);
                console.assert(response.code != null, '返回数据没有code字段');
                console.assert(response.msg != null, '返回数据没有msg字段');
                console.assert(response.data != null, '返回数据没有data字段');
                if (response.code == -1 && !response.msg) {
                    response.msg =   'No internet connection. Please check your network.';
                }
                success(response);
                if(typeof(jsonParam.loading) == "undefined") {
                    $(".loadingDiv").removeClass("active");
                }
            });
        } ,

        code : {
            error: -1,
            countineActivity: 10000
        }
    }
});